var id = document.getElementById("id");
var uName = document.getElementById("name");
var pwd = document.getElementById("pwd");
var tpwd = document.getElementById("tpwd");
var birth = document.getElementById("birthday");
var radioName = document.getElementsByName("g1");
var area = document.getElementById("area");
var email = document.getElementById("email");
var address = document.getElementById("address");
var tel = document.getElementById("tel");
var comment = document.getElementById("comment");
var msg = document.getElementsByClassName("errormsg");
var msgId = msg[0];
var msgName = msg[1];
var msgPwd = msg[2];
var msgTpwd = msg[3];
var msgBirth = msg[4];
var msgEmail = msg[5];
var msgTel = msg[6];
var spanS = document.getElementsByTagName("span");
var checkId = false;
var checkName = false;
var checkPwd = false;
var checkTpwd = false;
var checkBirth = false;
var checkEmail = false;
var checkTel = false;

id.onkeyup = function () {
    removeClass(msgId,"s1");
    if (id.value.length >= 6 && id.value.length <= 8){
        msgId.innerHTML = "";
        checkId = true;
    }else if(id.value.length == 0){
        msgId.innerHTML = "用户ID不能为空！";
    }else {
        msgId.innerHTML = "用户ID必须为6~8位!";
    }
    forPwd();
}
uName.onkeyup = function () {
    removeClass(msgName,"s1");
    if (uName.value.length >= 2 && uName.value.length <= 10){
        msgName.innerHTML = "";
        checkName = true;
    }else if(uName.value.length == 0){
        msgName.innerHTML = "用户名不能为空！";
    }else {
        msgName.innerHTML = "用户名必须为2~10位!";
    }
}
function forPwd(){
    if (pwd.value.length >= 6 && pwd.value.length <= 8){
        if (pwd.value == id.value){
            msgPwd.innerHTML = "用户口令不能与用户ID相同！";
        }else{
            msgPwd.innerHTML = "";
            checkPwd = true;
        }

    }else if(pwd.value.length == 0){
        msgPwd.innerHTML = "口令不能为空！";
    }else {
        msgPwd.innerHTML = "口令必须为6~8位!";
    }
}
pwd.onkeyup = function () {
    removeClass(msgPwd,"s1");
    forPwd();
}

tpwd.onkeyup = function () {
    removeClass(msgTpwd,"s1");
    if(tpwd.value != pwd.value){
        msgTpwd.innerHTML = "确定口令必须与口令相同！";
    }else {
        msgTpwd.innerHTML = "";
        checkTpwd = true;
    }
}
birth.onkeyup = function () {
    removeClass(msgBirth,"s1");
    var r = new RegExp(/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/);
    if(birth.value != 0){
        if(r.test(birth.value)){
            msgBirth.innerHTML = "";
            checkBirth = true;
        }else {
            msgBirth.innerHTML = "格式必须为yyyy-mm-dd";
        }
    }else {
        msgBirth.innerHTML = "生日不能为空！";
    }

}
email.onkeyup = function() {
    removeClass(msgEmail,"s1");
    var e = new RegExp(/^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/);
    if (email.value != 0){
        if (e.test(email.value)){
            msgEmail.innerHTML = "";
            checkEmail = true;
        }else {
            msgEmail.innerHTML = "邮件格式错误！";
        }
    }else {
        msgEmail.innerHTML = "邮件不能为空！";
    }
}
tel.onkeyup = function () {
    removeClass(msgTel,"s1");
    var t = new RegExp(/^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/);
    if(t.test(tel.value)){
        msgTel.innerHTML = "";
        checkTel = true;
    }else {
        msgTel.innerHTML = "电话号码格式错误！";
    }
}
function removeClass(ele,txt){
    var str =  ele.className,
        index = str.indexOf(txt);
    if(index > -1){

        ele.className = str.replace(txt,"");

    }

}
var r1 = document.getElementById("r1");
var r2 = document.getElementById("r2");
var r3 = document.getElementById("r3");
var r4 = document.getElementById("r4");
var checkR = false;
function checkRadio(){
    if(r1.checked || r2.checked || r3.checked || r4.checked){
        checkR = true;
    }
}
/*function show() {
    for(var i = 0;i < radioName.length; i++){
        if(radioName[i].checked){
            var isChecked = radioName[i].value;
        }
    }
    var tb = document.getElementById("tb");
    var str = "<tr><td>" + id.value + "</td><td>" + uName.value + "</td><td>"
    + pwd.value + "</td><td>" + birth.value + "</td><td>" + isChecked + "</td><td>"
    + area.value + "</td><td>" + email.value + "</td><td>" + address.value + "</td><td>"
    + tel.value + "</td><td>" + comment.value + "</td></tr>";
    tb.innerHTML += str;
}*/

function check() {
    checkRadio();
if (checkId && checkName && checkPwd && checkTpwd && checkBirth && checkR && checkEmail && checkTel){
    return true;
}else {
    alert("提交失败！");
    return false;

}
}


